package com.lzx.java.smack.concurrent;

import static org.junit.Assert.*;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.jivesoftware.smack.filter.ThreadFilter;
import org.junit.Test;

public class ScheduledThreadPoolExecutorTest {

	@Test
	public void test() throws InterruptedException {
		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
				10);
		executor.schedule(new Runnable() {

			@Override
			public void run() {
				System.out.println(TimeUnit.MICROSECONDS.toSeconds(System
						.currentTimeMillis()));

			}
		}, 0, TimeUnit.SECONDS);

		// executor.scheduleAtFixedRate(new Runnable() {
		//
		// @Override
		// public void run() {
		// System.out.println(TimeUnit.MILLISECONDS.toSeconds(System
		// .currentTimeMillis()));
		// try {
		// Thread.sleep(2000L);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// }
		// }, 0, 1, TimeUnit.SECONDS);

		executor.scheduleWithFixedDelay(new Runnable() {

			@Override
			public void run() {
				System.out.println(TimeUnit.MILLISECONDS.toSeconds(System
						.currentTimeMillis()));
				try {
					Thread.sleep(2000L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}, 0, 1, TimeUnit.SECONDS);

		// executor.scheduleWithFixedDelay(command, initialDelay, delay, unit)

		Thread.sleep(10000L);
	}

	@Test
	public void test2() throws InterruptedException {
		Callable<String> callable = new Callable<String>() {

			@Override
			public String call() throws Exception {
				
				System.out.println(System.in.read());
				System.out.println("finished...");
				return "finished";
			}
		};
		
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		Future<String> future = executorService.submit(callable);
		System.out.println("isCancelled:" + future.isCancelled());
		System.out.println("isDone:" + future.isDone());
		System.out.println(future);
		System.out.println("cancel:" + future.cancel(true));
		Thread.sleep(5000);
		System.out.println("isCancelled:" + future.isCancelled());
		System.out.println("isDone:" + future.isDone());
		
		FutureTask f;

	}

}
